package chances.epg.data.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import chances.epg.commons.log.EpgLogFactory;
import chances.epg.data.PageBean;
import chances.epg.entity.biz.Biz;
import chances.epg.entity.biz.TemplateBind;
import chances.epg.entity.biz.TemplateParamInst;


@Service
public class BizService extends EpgService {
	private static Logger logger = EpgLogFactory.getSystemLogger();
	
	private Map<String,Biz> bizList;
	
	public BizService() {
		bizList = new ConcurrentHashMap<String,Biz>();
	}
	
	@PostConstruct
	@Scheduled(cron="0 */2 * * * ?")
	public void loadDatas() {
		this.loadAllBizs();
	}
	/**
	 * 后台定时刷新，装载展示栏目到内存中.
	 * 
	 */
	@SuppressWarnings("unchecked")
	public void loadAllBizs() {
		logger.debug("Loading bizs...");
		Map<String,Biz> result = new ConcurrentHashMap<String,Biz>();
		List<Biz> allBiz = (List<Biz>)this.queryForList("getAllBizs");
		for (Biz biz : allBiz) {
			result.put(biz.getCode(), biz);
		}
		
		Map<String,Biz> oldMaps = this.bizList;
		this.bizList = result;
		oldMaps.clear();
		oldMaps = null;
	}
	
	/**
	 * 获取展示频道. 
	 * @param code
	 * @return
	 */
	public Biz getBizByCode(String code) {
		//内存中查找
		Biz cacheBiz = this.bizList.get(code);
		if(cacheBiz != null) {
			return cacheBiz;
		}
		
		return this.findBizByCode(code);
	}
	
	@SuppressWarnings("unchecked")
	private Biz findBizByCode(String code) {
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("code", code);
		try{
			List<Biz> result = (List<Biz>)this.queryForList("getBizByCode",paramsMap);
		    return result.size() > 0 ? result.get(0) : null;
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	/**
	 * 获取展示频道的子频道
	 * @param code
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Biz> getChildrenOfBiz(String code){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("code", code);
		
		try{
		    return (List<Biz>)this.queryForList("getChildBiz", paramsMap);
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	/**
	 * 获取展示频道的子频道
	 * @param code
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Biz> getChildrenOfBiz(String code,int start,PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("code", code);
		
		try{
		    return (List<Biz>)this.queryForList("getChildBiz", paramsMap, start, pageBean);
		}finally{
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	/**
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TemplateBind> getAllTemplateBind() {
		return (List<TemplateBind>)this.queryForList("getAllTemplateBind");
	}
	
	/**
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TemplateParamInst> getAllTemplateParameter(){
		return (List<TemplateParamInst>)this.queryForList("getAllTemplateParameter");
	}
	
	   /**
     * 获取一级展示频道. 
     * @param code
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Biz> getRootBizList() {
        return (List<Biz>)this.queryForList("getRootBiz");
    }

	public Map<String, Biz> getBizList() {
		return bizList;
	}
}